package com.gitee.fsmxm.thinking.in.spring.dependency.injection;

import com.gitee.fsmxm.thinking.in.spring.dependency.domain.UserHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * api方式 构造器方法 依赖注入 demo
 */
public class ApiDependencyConstructorInjectionDemo {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        applicationContext.register(ApiDependencyConstructorInjectionDemo.class);

        BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);

        beanDefinitionReader.loadBeanDefinitions("classpath:/META-INF/dependency-lookup-context.xml");

        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(UserHolder.class);

        // 构造器  参数的顺序是固定的
        beanDefinitionBuilder.addConstructorArgReference("user");

        AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();

        applicationContext.registerBeanDefinition("userHolder", beanDefinition);

        applicationContext.refresh();

        System.out.println(applicationContext.getBean(UserHolder.class));

    }

}
